﻿using System.Collections.Generic;
using Yz.Base;

namespace Yz.Core.Extensions
{
    public static class VmReturnExtension
    {
        /// <summary>
        /// 把操作code转换成VmReturn
        /// </summary>
        /// <param name="retCode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmReturn ToVmReturn(this ReturnCode retCode, string msg)
        {
            if (retCode == ReturnCode.NoExit || retCode == ReturnCode.Success)
            {
                return new VmReturn() { suc = true, msg = msg };
            }
            return new VmReturn() { suc = false, msg = msg };
        }
        /// <summary>
        /// 把操作code转换成VmReturn
        /// </summary>
        /// <param name="retCode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmReturn ToVmReturn(this ReturnCode retCode)
        {
            if (retCode == ReturnCode.NoExit)
            {
                return new VmReturn() { suc = true, msg = "您要操作的数据不存在或者您没有权限操作！" };
            }
            else if (retCode == ReturnCode.Exit)
            {
                return new VmReturn() { suc = true, msg = "您要操作的数据已存在！" };
            }
            else if (retCode == ReturnCode.Success)
            {
                return new VmReturn() { suc = true, msg = "操作成功！" };
            }
            return new VmReturn() { suc = false, msg = "操作失败" };
        }

        public static VmPageList<T> ToVmPageList<T>(this ReturnList<T> list) where T : ViewModel
        {
            return new VmPageList<T>()
            {
                total = list.Total,
                list = list.List,
                suc = list.Total > 0,
                msg = list.Total > 0 ? "" : "没有查询到数据！"
            };
        }

        public static VmList<T> ToVmList<T>(this ReturnList<T> list) where T : ViewModel
        {
            return new VmList<T>()
            {
                list = list.List,
                suc = list.List.Count > 0,
                msg = list.List.Count > 0 ? "" : "没有查询到数据！"
            };
        }

        public static VmData<T> ToVmData<T>(this T data) where T : ViewModel
        {
            return new VmData<T>() { data = data, suc = data != null, msg = data == null ? "数据不存在！" : "" };
        }

        public static VmCount ToVmCount(this int total)
        {
            return new VmCount() { suc = total > 0, total = total, msg = total > 0 ? "" : "数量为0！" };
        }

        public static VmList<T> ToVmList<T>(this List<T> list) where T : ViewModel
        {
            return new VmList<T>()
            {
                list = list,
                suc = list.Count > 0,
                msg = list.Count > 0 ? "" : "没有查询到数据！"
            };
        }
    }
}
